#include <iostream>
#include <map>
using namespace std;

const int kMaxN = 2e5 + 10;

int a[kMaxN];

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int T;
  for (cin >> T; T; T--) {
    int n;
    map<pair<int, int>, int> pair_map[3];
    map<tuple<int, int, int>, int> triple;
    long long ans = 0;

    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (int i = 0; i + 2 < n; i++) {
      ans += pair_map[0][make_pair(a[i + 1], a[i + 2])]++;
      ans += pair_map[1][make_pair(a[i], a[i + 2])]++;
      ans += pair_map[2][make_pair(a[i], a[i + 1])]++;
      ans -= 3 * triple[make_tuple(a[i], a[i + 1], a[i + 2])]++;
    }
    cout << ans << "\n";
  }
  return 0;
}